package com.blb.community_admin.controller;

import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysMenu;
import com.blb.community_admin.entity.vo.MenuVO;
import com.blb.community_admin.service.ISysMenuService;
import com.blb.community_admin.util.MenuTree;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Api
@RestController
public class MenuController {

//    @Resource
//    private ISysUserService sysUserService;

    @Resource
    private ISysMenuService sysMenuService;

    @ApiOperation("根据用户名查询菜单")
    @GetMapping("/menus")
    public ResponseResult<List<MenuVO>> getMenusByUsername(String username){
//        List<SysMenu> menusByUsername = sysUserService.getMenusByUsername(username);
        List<MenuVO> menuPage = sysMenuService.getMenuPage(username);
        System.out.println(menuPage);
        return ResponseResult.ok(menuPage);
    }

    @ApiOperation("查询菜单列表")
    @GetMapping("/getMenus")
    public ResponseResult<List<MenuVO>> getMenusPage(){
//        IPage<SysMenu> menuPage = sysMenuService.getMenuPage(current, size);
        return ResponseResult.ok(sysMenuService.getAllMenu());
    }

    @ApiOperation("按条件查询菜单列表")
    @GetMapping("/getMenusByTerm")
    public ResponseResult<List<MenuVO>> getMenuBymenuName(String menuName,String status){
//        String menuName = sysMenu.getMenuName();
//        String values = sysMenu.getStatus();
//        System.out.println(menuName+values);
        List<MenuVO> menuByName = sysMenuService.getMenuByName(menuName, status);
        List<MenuVO> menuVOS = new MenuTree(menuByName).buildTree();
        return ResponseResult.ok(menuVOS);
    }

    @Log(module = "菜单管理", operationType = 1, requestMethod = "POST")
    @ApiOperation("新增菜单")
    @PostMapping("/addMenus")
    public ResponseResult<String> addMenus(@RequestBody SysMenu sysMenu){
        sysMenu.setCreateTime(LocalDateTime.now());
        sysMenuService.save(sysMenu);
        return ResponseResult.ok("add");
    }

    @Log(module = "菜单管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改菜单")
    @PutMapping("/updateMenus")
    public ResponseResult<String> updateMenus(@RequestBody SysMenu sysMenu){
        sysMenu.setUpdateTime(LocalDateTime.now());
        sysMenuService.updateById(sysMenu);
        return ResponseResult.ok("update");
    }

    @Log(module = "菜单管理", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除菜单")
    @DeleteMapping("/deleteMenus/{menuId}")
    public ResponseResult<String> deleteMenus(@PathVariable long menuId){
        sysMenuService.removeById(menuId);
        return ResponseResult.ok("delete");
    }

}
